﻿using Abp.AspNetCore.Mvc.Controllers;
using Mt.Site.Core.Base.Domain.Entities.ShortId;
using Mt.Site.Core.Navigations;
using Mt.Site.Core.Url;
using Mt.Site.Core.Web;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Mt.Site.Web.Controllers
{
    //[DisableAuditing]
    public abstract class MtSiteFrontEndControllerBase : AbpController
    {
        public ITenancyNameFinder TenancyNameFinder { get; set; }

        public IShortIdManager ShortIdManager { get; set; }

        private static char[] _splitChar = "/".ToCharArray();

        protected MtSiteFrontEndControllerBase() 
        {
            LocalizationSourceName = MtSiteConsts.LocalizationFrontendSourceName;
        }

        /// <summary>
        /// 频道页、内容页等不需要id、也不需要page
        /// </summary>
        public void CheckParametersForNormalPage()
        {
            //"{tenancyName}/{cultureName}/{controller}/{action}/{kind}"

            var absolutePath = UrlChecker.GetAuthority(Request);
            var list = absolutePath.Split(_splitChar, StringSplitOptions.RemoveEmptyEntries);
            if (list.Length > 5)
            {
                throw new ArgumentException("wrong url");
            }
        }

        /// <summary>
        /// 列表页不需要id参数,移动端的列表页不需要显式传入page
        /// </summary>
        /// <param name="isMobilePage">_menuStore.GetMenuFrontEndView(item).PcOrMobile</param>
        /// <param name="page"></param>
        /// <param name="infoCount"></param>
        public void CheckParametersForListPage(DeviceType isMobilePage, int page, int infoCount)
        {
            //"{tenancyName}/{cultureName}/{controller}/{action}/{kind}/{page}"， page可能缺失

            if (isMobilePage == DeviceType.Moblie || page == 1)
            {
                CheckParametersForNormalPage();
            }
            else if (infoCount == 0)
            {
                throw new ArgumentException("wrong url");
            }
        }
    }
}
